﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class TaskMgtBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPickDetailDA pickDetailDA = DataAccess.CreatePickDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly ITaskDetailDA taskDetail = DataAccess.CreateTaskDetailDA();

        public TaskMgtBLL()
        {
            this.pubParasInfo = null;
        }

        public TaskMgtBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TaskMgtBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteTaskDetail(List<string> taskDetailID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string whLoginID = this.dataBaseID[0];
            string temp = string.Empty;
            InventoryMgtBLL inventoryMgtBll = new InventoryMgtBLL();
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            try
            {
                for (int i = 0; i < taskDetailID.Count; i++)
                {
                    TaskDetailInfo taskDetailInfo = taskDetail.GetTaskDetailByID(dataBase, tran, taskDetailID[i], whLoginID, this.pubParasInfo);
                    if ((taskDetailInfo != null) && (taskDetailInfo.TaskType == TaskType.PA))
                    {
                        inventoryMgtBll.UpdateInvSkuLocExceptedQty(dataBase, tran, taskDetailInfo.OwnerID, taskDetailInfo.SkuID, taskDetailInfo.ToLoc, taskDetailInfo.Qty * -1M, this.pubParasInfo.UserID, whLoginID);
                        inventoryMgtBll.UpdateInvLotLocLpnPendingQty(dataBase, tran, taskDetailInfo.Lot, taskDetailInfo.FromLoc, taskDetailInfo.FromLpnID, taskDetailInfo.Qty * -1M, this.pubParasInfo.UserID, whLoginID);
                    }
                    else if ((taskDetailInfo != null) && (taskDetailInfo.TaskType == TaskType.PK))
                    {
                        pickDetailDA.UpdatePickDetailStatus(dataBase, tran, taskDetailInfo.PickDetailID, PickDetailStatus.New, this.pubParasInfo.UserID, whLoginID);
                    }
                    if (taskDetailInfo.Status == TaskStatus.Pending)
                    {
                        List<string> tempList = new List<string> {
                            taskDetailID[i]
                        };
                        if (taskDetail.DeleteTaskDetail(dataBase, tran, tempList, whLoginID) > 0)
                        {
                            auditDataBLL.InsertAuditData<TaskDetailInfo>(dataBase, tran, taskDetailInfo, null, AuditAction.Delete, this.pubParasInfo.UserID, "TASK_DETAIL", this.dataBaseID[0]);
                            temp = temp + taskDetailID[i] + ",";
                        }
                    }
                    else
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F6050_001", this.pubParasInfo.UserLanguage);
                    }
                }
                tran.Commit();
                temp = string.IsNullOrEmpty(temp) ? "" : temp.Substring(0, temp.Length - 1);
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllTaskDetail()
        {
            return taskDetail.GetAllTaskDetail(this.dataBaseID[0], this.pubParasInfo);
        }

        public TaskDetailInfo GetTaskDetailByID(string taskdetailID)
        {
            return taskDetail.GetTaskDetailByID(taskdetailID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetTaskDetailByQueryList(TaskDetailQueryEntity taskdetailQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(taskdetailQuery.TaskDetailID))
            {
                sqlWhere.Add(" TASK_DETAIL_ID " + taskdetailQuery.TaskDetailID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.TaskType))
            {
                sqlWhere.Add(" TASK_TYPE " + taskdetailQuery.TaskType);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.DocID))
            {
                sqlWhere.Add(" DOC_ID " + taskdetailQuery.DocID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.DocLineID))
            {
                sqlWhere.Add(" DOC_LINE_ID " + taskdetailQuery.DocLineID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.ToLoc))
            {
                sqlWhere.Add(" TO_LOC " + taskdetailQuery.ToLoc);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.FromLoc))
            {
                sqlWhere.Add(" FROM_LOC " + taskdetailQuery.FromLoc);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.OwnerID))
            {
                sqlWhere.Add(" TD.OWNER_ID " + taskdetailQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + taskdetailQuery.SkuID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.Priority))
            {
                sqlWhere.Add(" PRIORITY " + taskdetailQuery.Priority);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.FromLpnID))
            {
                sqlWhere.Add(" TO_LPN_ID " + taskdetailQuery.FromLpnID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.Qty))
            {
                sqlWhere.Add(" Qty " + taskdetailQuery.Qty);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.ToLpnID))
            {
                sqlWhere.Add(" TO_LPN_ID " + taskdetailQuery.ToLpnID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.UserID))
            {
                sqlWhere.Add(" USER_ID " + taskdetailQuery.UserID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.UserID))
            {
                sqlWhere.Add(" USER_ID " + taskdetailQuery.UserID);
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.StartTime))
            {
                sqlWhere.Add(taskdetailQuery.StartTime.Replace("@BeginTime@", " START_TIME "));
            }
            if (!string.IsNullOrEmpty(taskdetailQuery.EndTime))
            {
                sqlWhere.Add(taskdetailQuery.EndTime.Replace("@EndTime@", " END_TIME "));
            }
            return taskDetail.GetTaskDetailByQueryList(sqlWhere, taskdetailQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertTaskDetail(TaskDetailInfo taskDetailInfo)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                taskDetailInfo.TaskDetailID = commonMethod.GenNextNumStr(dataBase, tran, "TASK_DETAIL", this.dataBaseID[0]);
                taskDetailInfo.CreatedDate = DateTime.Now;
                taskDetailInfo.UpdatedDate = DateTime.Now;
                taskDetailInfo.UpdatedBy = taskDetailInfo.CreatedBy;
                taskDetail.InsertTaskDetail(dataBase, tran, taskDetailInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<TaskDetailInfo>(dataBase, tran, null, taskDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TASK_DETAIL", this.dataBaseID[0]);
                tran.Commit();
                temp = taskDetailInfo.TaskDetailID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string UpdateTaskDetail(TaskDetailInfo taskDetailInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                TaskDetailInfo oldTaskDetailInfo = taskDetail.GetTaskDetailByID(dataBase, tran, taskDetailInfo.TaskDetailID, this.dataBaseID[0], this.pubParasInfo);
                taskDetail.UpdateTaskDetail(dataBase, tran, taskDetailInfo, this.dataBaseID[0]);
                new AuditDataBLL().InsertAuditData<TaskDetailInfo>(oldTaskDetailInfo, taskDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "TASK_DETAIL", this.dataBaseID[0]);
                tran.Commit();
                return taskDetailInfo.TaskDetailID;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return "";
        }
    }
}

